using System;
using System.Configuration;
using System.Web;
using System.Xml;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2005-09-08
	Status:		release
	Version:	2.0.0
	Build:		20070524
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20070124:	This class provides a utility class for xml configuration sections in 
				a web.config file, rather than using the appSettings element. It also 
				allows more complex configurations using nested xml elements.
				The web.config will typically require the following:
				<configSections>
				   <section name="engineX" type="umlungu.engineX.x_config,engineX" />
				</configSections>
	20070524:	Modification for .Net 2.0
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX {
	/// <summary>
	/// umlungu EngineX class. 
	/// class for the EngineX status xml.
	/// </summary>
	public class x_config {
		#region Invisible properties
		private XmlDocument configXml;
		#endregion

		#region String constants
		private const string error_config_path = "Could not load configuration for: ";
		#endregion

		#region Visible properties
		private string name = "engineX";
		/// <summary>Definiton of the property</summary>
		/// <value>Description of the value of the property</value>
		public string Name {
			get { return name; }
			set { name = value; }
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public x_config() {
			initialise();
		}
		/// <summary>Constructor description</summary>
		/// <param name="ConfigName">Name of the configuration section</param>
		public x_config(string ConfigName) {
			name = ConfigName;
			initialise();
		}
		#endregion

		#region Public methods
		/// <summary>Gets the value of an element in the configuration identified by an xpath expression</summary>
		/// <param name="path">The xpath expression</param>
		public string Value(string path) {
			try {
				XmlNode thisnode = configXml.DocumentElement.SelectSingleNode(path);
				return (thisnode == null) ? null : thisnode.InnerText; 
			} catch {
				throw new x_exception("error_config_path", String.Concat(error_config_path, path));
			}
		}
		/// <summary>Gets an element in the configuration identified by an xpath expression</summary>
		/// <param name="path">The xpath expression</param>
		public XmlElement Element(string path) {
			try {
				XmlNode thisnode = configXml.DocumentElement.SelectSingleNode(path);
				return (thisnode == null) ? null : thisnode as XmlElement; 
			} catch {
				throw new x_exception("error_config_path", String.Concat(error_config_path, path));
			}
		}
		#endregion
		
		#region Protected methods
		#endregion
				
		#region Private methods
		/// <summary>
		/// Description of this method
		/// </summary>
		private void initialise() {
			configXml = new XmlDocument();
			XmlElement configElement = ConfigurationManager.GetSection(name) as XmlElement;
			configXml.LoadXml(configElement.OuterXml);
		}
		#endregion
	}
}
